#include <time.h>
#include "unp.h"

int main(int argc, char *argv[])
{
    int srvfd, connfd, addrlen;
    struct sockaddr_in srvaddr, cliaddr;
    char buf[MAXLINE];
    time_t now;

    srvfd = Socket(AF_INET, SOCK_STREAM, 0);

    bzero(&srvaddr, sizeof(srvaddr));
    srvaddr.sin_family = AF_INET;
    srvaddr.sin_port = htons(8000);
    srvaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    Bind(srvfd, (SA*)&srvaddr, sizeof(srvaddr));
    Listen(srvfd, 15);
    
    while (1) {
        connfd = Accept(srvfd, (SA*)&cliaddr, &addrlen);
        time(&now);
        snprintf(buf, sizeof(buf), "%s", ctime(&now));
        print_hex("buf", buf, strlen(buf));
        printf("It is %s now", buf);
        if (0) {
            int i;
            for (i=0; i<strlen(buf); i++) {
                if (i == 5) sleep(10);
                Send(connfd, &buf[i], 1, 0);
            }
        } else {
            Send(connfd, buf, strlen(buf), 0);
        }
        Close(connfd);
    }
    
    return 0;
}
